﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Weight.Model;
using Dapper;
using System.Data;
using Weight.Base;

namespace Weight.Dal
{
    public class DalTableField
    {
        /// <summary>
        /// 获取指定表的所有字段
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public IEnumerable<TableFieldEntity> GetTableField(string tableName)
        {
            using (IDbConnection conn = SessionFactory.CreateConnection())
            {
                string sql = @" select  b.name as Field, CONVERT(NVARCHAR(100),isnull(c.[value],'-'))  as Decribe,(case  t.name when 'nvarchar' then '字符串' 
                 when 'varbinary' then '字符串' when 'varchar' then '字符串' when 'int' then '整数'  when 'float' then '数字' when'decimal' then '数字'  when 'datetime' then '日期时间'
                 when 'bit' then '布尔类型'
                 else '' end) as DataType     
                 from sys.tables a
                 inner join sys.columns b on a.object_id = b.object_id
	             inner join  systypes t on b.user_type_id = t.xusertype
                 left join sys.extended_properties c on c.major_id = b.object_id AND c.minor_id = b.column_id and  c.name='MS_Description'
                  where a.name =@tableName";
                return conn.Query<TableFieldEntity>(sql,new { tableName });
            }
        }

    }
}
